<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Source code</title>
<link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">
</head>
<body>
<main role="main">
<div class="sourceContainer">
<pre><span class="sourceLineNo">001</span><a id="line.1">/*</a>
<span class="sourceLineNo">002</span><a id="line.2"> * Licensed to the Apache Software Foundation (ASF) under one or more</a>
<span class="sourceLineNo">003</span><a id="line.3"> * contributor license agreements.  See the NOTICE file distributed with</a>
<span class="sourceLineNo">004</span><a id="line.4"> * this work for additional information regarding copyright ownership.</a>
<span class="sourceLineNo">005</span><a id="line.5"> * The ASF licenses this file to You under the Apache License, Version 2.0</a>
<span class="sourceLineNo">006</span><a id="line.6"> * (the "License"); you may not use this file except in compliance with</a>
<span class="sourceLineNo">007</span><a id="line.7"> * the License.  You may obtain a copy of the License at</a>
<span class="sourceLineNo">008</span><a id="line.8"> *</a>
<span class="sourceLineNo">009</span><a id="line.9"> *      http://www.apache.org/licenses/LICENSE-2.0</a>
<span class="sourceLineNo">010</span><a id="line.10"> *</a>
<span class="sourceLineNo">011</span><a id="line.11"> * Unless required by applicable law or agreed to in writing, software</a>
<span class="sourceLineNo">012</span><a id="line.12"> * distributed under the License is distributed on an "AS IS" BASIS,</a>
<span class="sourceLineNo">013</span><a id="line.13"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</a>
<span class="sourceLineNo">014</span><a id="line.14"> * See the License for the specific language governing permissions and</a>
<span class="sourceLineNo">015</span><a id="line.15"> * limitations under the License.</a>
<span class="sourceLineNo">016</span><a id="line.16"> */</a>
<span class="sourceLineNo">017</span><a id="line.17">package org.apache.commons.dbutils;</a>
<span class="sourceLineNo">018</span><a id="line.18"></a>
<span class="sourceLineNo">019</span><a id="line.19">import java.sql.CallableStatement;</a>
<span class="sourceLineNo">020</span><a id="line.20">import java.sql.Connection;</a>
<span class="sourceLineNo">021</span><a id="line.21">import java.sql.PreparedStatement;</a>
<span class="sourceLineNo">022</span><a id="line.22">import java.sql.ResultSet;</a>
<span class="sourceLineNo">023</span><a id="line.23">import java.sql.SQLException;</a>
<span class="sourceLineNo">024</span><a id="line.24">import java.sql.Statement;</a>
<span class="sourceLineNo">025</span><a id="line.25">import java.util.LinkedList;</a>
<span class="sourceLineNo">026</span><a id="line.26">import java.util.List;</a>
<span class="sourceLineNo">027</span><a id="line.27">import javax.sql.DataSource;</a>
<span class="sourceLineNo">028</span><a id="line.28"></a>
<span class="sourceLineNo">029</span><a id="line.29">/**</a>
<span class="sourceLineNo">030</span><a id="line.30"> * Executes SQL queries with pluggable strategies for handling</a>
<span class="sourceLineNo">031</span><a id="line.31"> * {@code ResultSet}s.  This class is thread safe.</a>
<span class="sourceLineNo">032</span><a id="line.32"> *</a>
<span class="sourceLineNo">033</span><a id="line.33"> * @see ResultSetHandler</a>
<span class="sourceLineNo">034</span><a id="line.34"> */</a>
<span class="sourceLineNo">035</span><a id="line.35">public class QueryRunner extends AbstractQueryRunner {</a>
<span class="sourceLineNo">036</span><a id="line.36"></a>
<span class="sourceLineNo">037</span><a id="line.37">    /**</a>
<span class="sourceLineNo">038</span><a id="line.38">     * Constructor for QueryRunner.</a>
<span class="sourceLineNo">039</span><a id="line.39">     */</a>
<span class="sourceLineNo">040</span><a id="line.40">    public QueryRunner() {</a>
<span class="sourceLineNo">041</span><a id="line.41">        super();</a>
<span class="sourceLineNo">042</span><a id="line.42">    }</a>
<span class="sourceLineNo">043</span><a id="line.43"></a>
<span class="sourceLineNo">044</span><a id="line.44">    /**</a>
<span class="sourceLineNo">045</span><a id="line.45">     * Constructor for QueryRunner that controls the use of {@code ParameterMetaData}.</a>
<span class="sourceLineNo">046</span><a id="line.46">     *</a>
<span class="sourceLineNo">047</span><a id="line.47">     * @param pmdKnownBroken Some drivers don't support {@link java.sql.ParameterMetaData#getParameterType(int) };</a>
<span class="sourceLineNo">048</span><a id="line.48">     * if {@code pmdKnownBroken} is set to true, we won't even try it; if false, we'll try it,</a>
<span class="sourceLineNo">049</span><a id="line.49">     * and if it breaks, we'll remember not to use it again.</a>
<span class="sourceLineNo">050</span><a id="line.50">     */</a>
<span class="sourceLineNo">051</span><a id="line.51">    public QueryRunner(final boolean pmdKnownBroken) {</a>
<span class="sourceLineNo">052</span><a id="line.52">        super(pmdKnownBroken);</a>
<span class="sourceLineNo">053</span><a id="line.53">    }</a>
<span class="sourceLineNo">054</span><a id="line.54"></a>
<span class="sourceLineNo">055</span><a id="line.55">    /**</a>
<span class="sourceLineNo">056</span><a id="line.56">     * Constructor for QueryRunner that takes a {@code DataSource} to use.</a>
<span class="sourceLineNo">057</span><a id="line.57">     *</a>
<span class="sourceLineNo">058</span><a id="line.58">     * Methods that do not take a {@code Connection} parameter will retrieve connections from this</a>
<span class="sourceLineNo">059</span><a id="line.59">     * {@code DataSource}.</a>
<span class="sourceLineNo">060</span><a id="line.60">     *</a>
<span class="sourceLineNo">061</span><a id="line.61">     * @param ds The {@code DataSource} to retrieve connections from.</a>
<span class="sourceLineNo">062</span><a id="line.62">     */</a>
<span class="sourceLineNo">063</span><a id="line.63">    public QueryRunner(final DataSource ds) {</a>
<span class="sourceLineNo">064</span><a id="line.64">        super(ds);</a>
<span class="sourceLineNo">065</span><a id="line.65">    }</a>
<span class="sourceLineNo">066</span><a id="line.66"></a>
<span class="sourceLineNo">067</span><a id="line.67">    /**</a>
<span class="sourceLineNo">068</span><a id="line.68">     * Constructor for QueryRunner that takes a {@code StatementConfiguration} to configure statements when</a>
<span class="sourceLineNo">069</span><a id="line.69">     * preparing them.</a>
<span class="sourceLineNo">070</span><a id="line.70">     *</a>
<span class="sourceLineNo">071</span><a id="line.71">     * @param stmtConfig The configuration to apply to statements when they are prepared.</a>
<span class="sourceLineNo">072</span><a id="line.72">     */</a>
<span class="sourceLineNo">073</span><a id="line.73">    public QueryRunner(final StatementConfiguration stmtConfig) {</a>
<span class="sourceLineNo">074</span><a id="line.74">        super(stmtConfig);</a>
<span class="sourceLineNo">075</span><a id="line.75">    }</a>
<span class="sourceLineNo">076</span><a id="line.76"></a>
<span class="sourceLineNo">077</span><a id="line.77">    /**</a>
<span class="sourceLineNo">078</span><a id="line.78">     * Constructor for QueryRunner that takes a {@code DataSource} and controls the use of {@code ParameterMetaData}.</a>
<span class="sourceLineNo">079</span><a id="line.79">     * Methods that do not take a {@code Connection} parameter will retrieve connections from this</a>
<span class="sourceLineNo">080</span><a id="line.80">     * {@code DataSource}.</a>
<span class="sourceLineNo">081</span><a id="line.81">     *</a>
<span class="sourceLineNo">082</span><a id="line.82">     * @param ds The {@code DataSource} to retrieve connections from.</a>
<span class="sourceLineNo">083</span><a id="line.83">     * @param pmdKnownBroken Some drivers don't support {@link java.sql.ParameterMetaData#getParameterType(int) };</a>
<span class="sourceLineNo">084</span><a id="line.84">     * if {@code pmdKnownBroken} is set to true, we won't even try it; if false, we'll try it,</a>
<span class="sourceLineNo">085</span><a id="line.85">     * and if it breaks, we'll remember not to use it again.</a>
<span class="sourceLineNo">086</span><a id="line.86">     */</a>
<span class="sourceLineNo">087</span><a id="line.87">    public QueryRunner(final DataSource ds, final boolean pmdKnownBroken) {</a>
<span class="sourceLineNo">088</span><a id="line.88">        super(ds, pmdKnownBroken);</a>
<span class="sourceLineNo">089</span><a id="line.89">    }</a>
<span class="sourceLineNo">090</span><a id="line.90"></a>
<span class="sourceLineNo">091</span><a id="line.91">    /**</a>
<span class="sourceLineNo">092</span><a id="line.92">     * Constructor for QueryRunner that takes a {@code DataSource} to use and a {@code StatementConfiguration}.</a>
<span class="sourceLineNo">093</span><a id="line.93">     *</a>
<span class="sourceLineNo">094</span><a id="line.94">     * Methods that do not take a {@code Connection} parameter will retrieve connections from this</a>
<span class="sourceLineNo">095</span><a id="line.95">     * {@code DataSource}.</a>
<span class="sourceLineNo">096</span><a id="line.96">     *</a>
<span class="sourceLineNo">097</span><a id="line.97">     * @param ds The {@code DataSource} to retrieve connections from.</a>
<span class="sourceLineNo">098</span><a id="line.98">     * @param stmtConfig The configuration to apply to statements when they are prepared.</a>
<span class="sourceLineNo">099</span><a id="line.99">     */</a>
<span class="sourceLineNo">100</span><a id="line.100">    public QueryRunner(final DataSource ds, final StatementConfiguration stmtConfig) {</a>
<span class="sourceLineNo">101</span><a id="line.101">        super(ds, stmtConfig);</a>
<span class="sourceLineNo">102</span><a id="line.102">    }</a>
<span class="sourceLineNo">103</span><a id="line.103"></a>
<span class="sourceLineNo">104</span><a id="line.104">    /**</a>
<span class="sourceLineNo">105</span><a id="line.105">     * Constructor for QueryRunner that takes a {@code DataSource}, a {@code StatementConfiguration}, and</a>
<span class="sourceLineNo">106</span><a id="line.106">     * controls the use of {@code ParameterMetaData}.  Methods that do not take a {@code Connection} parameter</a>
<span class="sourceLineNo">107</span><a id="line.107">     * will retrieve connections from this {@code DataSource}.</a>
<span class="sourceLineNo">108</span><a id="line.108">     *</a>
<span class="sourceLineNo">109</span><a id="line.109">     * @param ds The {@code DataSource} to retrieve connections from.</a>
<span class="sourceLineNo">110</span><a id="line.110">     * @param pmdKnownBroken Some drivers don't support {@link java.sql.ParameterMetaData#getParameterType(int) };</a>
<span class="sourceLineNo">111</span><a id="line.111">     * if {@code pmdKnownBroken} is set to true, we won't even try it; if false, we'll try it,</a>
<span class="sourceLineNo">112</span><a id="line.112">     * and if it breaks, we'll remember not to use it again.</a>
<span class="sourceLineNo">113</span><a id="line.113">     * @param stmtConfig The configuration to apply to statements when they are prepared.</a>
<span class="sourceLineNo">114</span><a id="line.114">     */</a>
<span class="sourceLineNo">115</span><a id="line.115">    public QueryRunner(final DataSource ds, final boolean pmdKnownBroken, final StatementConfiguration stmtConfig) {</a>
<span class="sourceLineNo">116</span><a id="line.116">        super(ds, pmdKnownBroken, stmtConfig);</a>
<span class="sourceLineNo">117</span><a id="line.117">    }</a>
<span class="sourceLineNo">118</span><a id="line.118"></a>
<span class="sourceLineNo">119</span><a id="line.119">    /**</a>
<span class="sourceLineNo">120</span><a id="line.120">     * Execute a batch of SQL INSERT, UPDATE, or DELETE queries.</a>
<span class="sourceLineNo">121</span><a id="line.121">     *</a>
<span class="sourceLineNo">122</span><a id="line.122">     * @param conn The Connection to use to run the query.  The caller is</a>
<span class="sourceLineNo">123</span><a id="line.123">     * responsible for closing this Connection.</a>
<span class="sourceLineNo">124</span><a id="line.124">     * @param sql The SQL to execute.</a>
<span class="sourceLineNo">125</span><a id="line.125">     * @param params An array of query replacement parameters.  Each row in</a>
<span class="sourceLineNo">126</span><a id="line.126">     * this array is one set of batch replacement values.</a>
<span class="sourceLineNo">127</span><a id="line.127">     * @return The number of rows updated per statement.</a>
<span class="sourceLineNo">128</span><a id="line.128">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">129</span><a id="line.129">     * @since DbUtils 1.1</a>
<span class="sourceLineNo">130</span><a id="line.130">     */</a>
<span class="sourceLineNo">131</span><a id="line.131">    public int[] batch(final Connection conn, final String sql, final Object[][] params) throws SQLException {</a>
<span class="sourceLineNo">132</span><a id="line.132">        return this.batch(conn, false, sql, params);</a>
<span class="sourceLineNo">133</span><a id="line.133">    }</a>
<span class="sourceLineNo">134</span><a id="line.134"></a>
<span class="sourceLineNo">135</span><a id="line.135">    /**</a>
<span class="sourceLineNo">136</span><a id="line.136">     * Execute a batch of SQL INSERT, UPDATE, or DELETE queries.  The</a>
<span class="sourceLineNo">137</span><a id="line.137">     * {@code Connection} is retrieved from the {@code DataSource}</a>
<span class="sourceLineNo">138</span><a id="line.138">     * set in the constructor.  This {@code Connection} must be in</a>
<span class="sourceLineNo">139</span><a id="line.139">     * auto-commit mode or the update will not be saved.</a>
<span class="sourceLineNo">140</span><a id="line.140">     *</a>
<span class="sourceLineNo">141</span><a id="line.141">     * @param sql The SQL to execute.</a>
<span class="sourceLineNo">142</span><a id="line.142">     * @param params An array of query replacement parameters.  Each row in</a>
<span class="sourceLineNo">143</span><a id="line.143">     * this array is one set of batch replacement values.</a>
<span class="sourceLineNo">144</span><a id="line.144">     * @return The number of rows updated per statement.</a>
<span class="sourceLineNo">145</span><a id="line.145">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">146</span><a id="line.146">     * @since DbUtils 1.1</a>
<span class="sourceLineNo">147</span><a id="line.147">     */</a>
<span class="sourceLineNo">148</span><a id="line.148">    public int[] batch(final String sql, final Object[][] params) throws SQLException {</a>
<span class="sourceLineNo">149</span><a id="line.149">        try (final Connection conn = this.prepareConnection()) {</a>
<span class="sourceLineNo">150</span><a id="line.150">            return this.batch(conn, true, sql, params);</a>
<span class="sourceLineNo">151</span><a id="line.151">        }</a>
<span class="sourceLineNo">152</span><a id="line.152">    }</a>
<span class="sourceLineNo">153</span><a id="line.153"></a>
<span class="sourceLineNo">154</span><a id="line.154">    /**</a>
<span class="sourceLineNo">155</span><a id="line.155">     * Calls update after checking the parameters to ensure nothing is null.</a>
<span class="sourceLineNo">156</span><a id="line.156">     * @param conn The connection to use for the batch call.</a>
<span class="sourceLineNo">157</span><a id="line.157">     * @param closeConn True if the connection should be closed, false otherwise.</a>
<span class="sourceLineNo">158</span><a id="line.158">     * @param sql The SQL statement to execute.</a>
<span class="sourceLineNo">159</span><a id="line.159">     * @param params An array of query replacement parameters.  Each row in</a>
<span class="sourceLineNo">160</span><a id="line.160">     * this array is one set of batch replacement values.</a>
<span class="sourceLineNo">161</span><a id="line.161">     * @return The number of rows updated in the batch.</a>
<span class="sourceLineNo">162</span><a id="line.162">     * @throws SQLException If there are database or parameter errors.</a>
<span class="sourceLineNo">163</span><a id="line.163">     */</a>
<span class="sourceLineNo">164</span><a id="line.164">    private int[] batch(final Connection conn, final boolean closeConn, final String sql, final Object[][] params) throws SQLException {</a>
<span class="sourceLineNo">165</span><a id="line.165">        if (conn == null) {</a>
<span class="sourceLineNo">166</span><a id="line.166">            throw new SQLException("Null connection");</a>
<span class="sourceLineNo">167</span><a id="line.167">        }</a>
<span class="sourceLineNo">168</span><a id="line.168"></a>
<span class="sourceLineNo">169</span><a id="line.169">        if (sql == null) {</a>
<span class="sourceLineNo">170</span><a id="line.170">            throw new SQLException("Null SQL statement");</a>
<span class="sourceLineNo">171</span><a id="line.171">        }</a>
<span class="sourceLineNo">172</span><a id="line.172"></a>
<span class="sourceLineNo">173</span><a id="line.173">        if (params == null) {</a>
<span class="sourceLineNo">174</span><a id="line.174">            throw new SQLException("Null parameters. If parameters aren't need, pass an empty array.");</a>
<span class="sourceLineNo">175</span><a id="line.175">        }</a>
<span class="sourceLineNo">176</span><a id="line.176"></a>
<span class="sourceLineNo">177</span><a id="line.177">        PreparedStatement stmt = null;</a>
<span class="sourceLineNo">178</span><a id="line.178">        int[] rows = null;</a>
<span class="sourceLineNo">179</span><a id="line.179">        try {</a>
<span class="sourceLineNo">180</span><a id="line.180">            stmt = this.prepareStatement(conn, sql);</a>
<span class="sourceLineNo">181</span><a id="line.181"></a>
<span class="sourceLineNo">182</span><a id="line.182">            for (Object[] param : params) {</a>
<span class="sourceLineNo">183</span><a id="line.183">                this.fillStatement(stmt, param);</a>
<span class="sourceLineNo">184</span><a id="line.184">                stmt.addBatch();</a>
<span class="sourceLineNo">185</span><a id="line.185">            }</a>
<span class="sourceLineNo">186</span><a id="line.186">            rows = stmt.executeBatch();</a>
<span class="sourceLineNo">187</span><a id="line.187"></a>
<span class="sourceLineNo">188</span><a id="line.188">        } catch (final SQLException e) {</a>
<span class="sourceLineNo">189</span><a id="line.189">            this.rethrow(e, sql, (Object[])params);</a>
<span class="sourceLineNo">190</span><a id="line.190">        } finally {</a>
<span class="sourceLineNo">191</span><a id="line.191">            close(stmt);</a>
<span class="sourceLineNo">192</span><a id="line.192">        }</a>
<span class="sourceLineNo">193</span><a id="line.193"></a>
<span class="sourceLineNo">194</span><a id="line.194">        return rows;</a>
<span class="sourceLineNo">195</span><a id="line.195">    }</a>
<span class="sourceLineNo">196</span><a id="line.196"></a>
<span class="sourceLineNo">197</span><a id="line.197">    /**</a>
<span class="sourceLineNo">198</span><a id="line.198">     * Execute an SQL SELECT query with a single replacement parameter. The</a>
<span class="sourceLineNo">199</span><a id="line.199">     * caller is responsible for closing the connection.</a>
<span class="sourceLineNo">200</span><a id="line.200">     * @param &lt;T&gt; The type of object that the handler returns</a>
<span class="sourceLineNo">201</span><a id="line.201">     * @param conn The connection to execute the query in.</a>
<span class="sourceLineNo">202</span><a id="line.202">     * @param sql The query to execute.</a>
<span class="sourceLineNo">203</span><a id="line.203">     * @param param The replacement parameter.</a>
<span class="sourceLineNo">204</span><a id="line.204">     * @param rsh The handler that converts the results into an object.</a>
<span class="sourceLineNo">205</span><a id="line.205">     * @return The object returned by the handler.</a>
<span class="sourceLineNo">206</span><a id="line.206">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">207</span><a id="line.207">     * @deprecated Use {@link #query(Connection, String, ResultSetHandler, Object...)}</a>
<span class="sourceLineNo">208</span><a id="line.208">     */</a>
<span class="sourceLineNo">209</span><a id="line.209">    @Deprecated</a>
<span class="sourceLineNo">210</span><a id="line.210">    public &lt;T&gt; T query(final Connection conn, final String sql, final Object param, final ResultSetHandler&lt;T&gt; rsh) throws SQLException {</a>
<span class="sourceLineNo">211</span><a id="line.211">        return this.&lt;T&gt;query(conn, false, sql, rsh, param);</a>
<span class="sourceLineNo">212</span><a id="line.212">    }</a>
<span class="sourceLineNo">213</span><a id="line.213"></a>
<span class="sourceLineNo">214</span><a id="line.214">    /**</a>
<span class="sourceLineNo">215</span><a id="line.215">     * Execute an SQL SELECT query with replacement parameters.  The</a>
<span class="sourceLineNo">216</span><a id="line.216">     * caller is responsible for closing the connection.</a>
<span class="sourceLineNo">217</span><a id="line.217">     * @param &lt;T&gt; The type of object that the handler returns</a>
<span class="sourceLineNo">218</span><a id="line.218">     * @param conn The connection to execute the query in.</a>
<span class="sourceLineNo">219</span><a id="line.219">     * @param sql The query to execute.</a>
<span class="sourceLineNo">220</span><a id="line.220">     * @param params The replacement parameters.</a>
<span class="sourceLineNo">221</span><a id="line.221">     * @param rsh The handler that converts the results into an object.</a>
<span class="sourceLineNo">222</span><a id="line.222">     * @return The object returned by the handler.</a>
<span class="sourceLineNo">223</span><a id="line.223">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">224</span><a id="line.224">     * @deprecated Use {@link #query(Connection,String,ResultSetHandler,Object...)} instead</a>
<span class="sourceLineNo">225</span><a id="line.225">     */</a>
<span class="sourceLineNo">226</span><a id="line.226">    @Deprecated</a>
<span class="sourceLineNo">227</span><a id="line.227">    public &lt;T&gt; T query(final Connection conn, final String sql, final Object[] params, final ResultSetHandler&lt;T&gt; rsh) throws SQLException {</a>
<span class="sourceLineNo">228</span><a id="line.228">        return this.&lt;T&gt;query(conn, false, sql, rsh, params);</a>
<span class="sourceLineNo">229</span><a id="line.229">    }</a>
<span class="sourceLineNo">230</span><a id="line.230"></a>
<span class="sourceLineNo">231</span><a id="line.231">    /**</a>
<span class="sourceLineNo">232</span><a id="line.232">     * Execute an SQL SELECT query with replacement parameters.  The</a>
<span class="sourceLineNo">233</span><a id="line.233">     * caller is responsible for closing the connection.</a>
<span class="sourceLineNo">234</span><a id="line.234">     * @param &lt;T&gt; The type of object that the handler returns</a>
<span class="sourceLineNo">235</span><a id="line.235">     * @param conn The connection to execute the query in.</a>
<span class="sourceLineNo">236</span><a id="line.236">     * @param sql The query to execute.</a>
<span class="sourceLineNo">237</span><a id="line.237">     * @param rsh The handler that converts the results into an object.</a>
<span class="sourceLineNo">238</span><a id="line.238">     * @param params The replacement parameters.</a>
<span class="sourceLineNo">239</span><a id="line.239">     * @return The object returned by the handler.</a>
<span class="sourceLineNo">240</span><a id="line.240">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">241</span><a id="line.241">     */</a>
<span class="sourceLineNo">242</span><a id="line.242">    public &lt;T&gt; T query(final Connection conn, final String sql, final ResultSetHandler&lt;T&gt; rsh, final Object... params) throws SQLException {</a>
<span class="sourceLineNo">243</span><a id="line.243">        return this.&lt;T&gt;query(conn, false, sql, rsh, params);</a>
<span class="sourceLineNo">244</span><a id="line.244">    }</a>
<span class="sourceLineNo">245</span><a id="line.245"></a>
<span class="sourceLineNo">246</span><a id="line.246">    /**</a>
<span class="sourceLineNo">247</span><a id="line.247">     * Execute an SQL SELECT query without any replacement parameters.  The</a>
<span class="sourceLineNo">248</span><a id="line.248">     * caller is responsible for closing the connection.</a>
<span class="sourceLineNo">249</span><a id="line.249">     * @param &lt;T&gt; The type of object that the handler returns</a>
<span class="sourceLineNo">250</span><a id="line.250">     * @param conn The connection to execute the query in.</a>
<span class="sourceLineNo">251</span><a id="line.251">     * @param sql The query to execute.</a>
<span class="sourceLineNo">252</span><a id="line.252">     * @param rsh The handler that converts the results into an object.</a>
<span class="sourceLineNo">253</span><a id="line.253">     * @return The object returned by the handler.</a>
<span class="sourceLineNo">254</span><a id="line.254">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">255</span><a id="line.255">     */</a>
<span class="sourceLineNo">256</span><a id="line.256">    public &lt;T&gt; T query(final Connection conn, final String sql, final ResultSetHandler&lt;T&gt; rsh) throws SQLException {</a>
<span class="sourceLineNo">257</span><a id="line.257">        return this.&lt;T&gt;query(conn, false, sql, rsh, (Object[]) null);</a>
<span class="sourceLineNo">258</span><a id="line.258">    }</a>
<span class="sourceLineNo">259</span><a id="line.259"></a>
<span class="sourceLineNo">260</span><a id="line.260">    /**</a>
<span class="sourceLineNo">261</span><a id="line.261">     * Executes the given SELECT SQL with a single replacement parameter.</a>
<span class="sourceLineNo">262</span><a id="line.262">     * The {@code Connection} is retrieved from the</a>
<span class="sourceLineNo">263</span><a id="line.263">     * {@code DataSource} set in the constructor.</a>
<span class="sourceLineNo">264</span><a id="line.264">     * @param &lt;T&gt; The type of object that the handler returns</a>
<span class="sourceLineNo">265</span><a id="line.265">     * @param sql The SQL statement to execute.</a>
<span class="sourceLineNo">266</span><a id="line.266">     * @param param The replacement parameter.</a>
<span class="sourceLineNo">267</span><a id="line.267">     * @param rsh The handler used to create the result object from</a>
<span class="sourceLineNo">268</span><a id="line.268">     * the {@code ResultSet}.</a>
<span class="sourceLineNo">269</span><a id="line.269">     *</a>
<span class="sourceLineNo">270</span><a id="line.270">     * @return An object generated by the handler.</a>
<span class="sourceLineNo">271</span><a id="line.271">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">272</span><a id="line.272">     * @deprecated Use {@link #query(String, ResultSetHandler, Object...)}</a>
<span class="sourceLineNo">273</span><a id="line.273">     */</a>
<span class="sourceLineNo">274</span><a id="line.274">    @Deprecated</a>
<span class="sourceLineNo">275</span><a id="line.275">    public &lt;T&gt; T query(final String sql, final Object param, final ResultSetHandler&lt;T&gt; rsh) throws SQLException {</a>
<span class="sourceLineNo">276</span><a id="line.276">        try (final Connection conn = this.prepareConnection()) {</a>
<span class="sourceLineNo">277</span><a id="line.277">            return this.&lt;T&gt;query(conn, true, sql, rsh, param);</a>
<span class="sourceLineNo">278</span><a id="line.278">        }</a>
<span class="sourceLineNo">279</span><a id="line.279">    }</a>
<span class="sourceLineNo">280</span><a id="line.280"></a>
<span class="sourceLineNo">281</span><a id="line.281">    /**</a>
<span class="sourceLineNo">282</span><a id="line.282">     * Executes the given SELECT SQL query and returns a result object.</a>
<span class="sourceLineNo">283</span><a id="line.283">     * The {@code Connection} is retrieved from the</a>
<span class="sourceLineNo">284</span><a id="line.284">     * {@code DataSource} set in the constructor.</a>
<span class="sourceLineNo">285</span><a id="line.285">     * @param &lt;T&gt; The type of object that the handler returns</a>
<span class="sourceLineNo">286</span><a id="line.286">     * @param sql The SQL statement to execute.</a>
<span class="sourceLineNo">287</span><a id="line.287">     * @param params Initialize the PreparedStatement's IN parameters with</a>
<span class="sourceLineNo">288</span><a id="line.288">     * this array.</a>
<span class="sourceLineNo">289</span><a id="line.289">     *</a>
<span class="sourceLineNo">290</span><a id="line.290">     * @param rsh The handler used to create the result object from</a>
<span class="sourceLineNo">291</span><a id="line.291">     * the {@code ResultSet}.</a>
<span class="sourceLineNo">292</span><a id="line.292">     *</a>
<span class="sourceLineNo">293</span><a id="line.293">     * @return An object generated by the handler.</a>
<span class="sourceLineNo">294</span><a id="line.294">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">295</span><a id="line.295">     * @deprecated Use {@link #query(String, ResultSetHandler, Object...)}</a>
<span class="sourceLineNo">296</span><a id="line.296">     */</a>
<span class="sourceLineNo">297</span><a id="line.297">    @Deprecated</a>
<span class="sourceLineNo">298</span><a id="line.298">    public &lt;T&gt; T query(final String sql, final Object[] params, final ResultSetHandler&lt;T&gt; rsh) throws SQLException {</a>
<span class="sourceLineNo">299</span><a id="line.299">        try (final Connection conn = this.prepareConnection()) {</a>
<span class="sourceLineNo">300</span><a id="line.300">            return this.&lt;T&gt;query(conn, true, sql, rsh, params);</a>
<span class="sourceLineNo">301</span><a id="line.301">        }</a>
<span class="sourceLineNo">302</span><a id="line.302">    }</a>
<span class="sourceLineNo">303</span><a id="line.303"></a>
<span class="sourceLineNo">304</span><a id="line.304">    /**</a>
<span class="sourceLineNo">305</span><a id="line.305">     * Executes the given SELECT SQL query and returns a result object.</a>
<span class="sourceLineNo">306</span><a id="line.306">     * The {@code Connection} is retrieved from the</a>
<span class="sourceLineNo">307</span><a id="line.307">     * {@code DataSource} set in the constructor.</a>
<span class="sourceLineNo">308</span><a id="line.308">     * @param &lt;T&gt; The type of object that the handler returns</a>
<span class="sourceLineNo">309</span><a id="line.309">     * @param sql The SQL statement to execute.</a>
<span class="sourceLineNo">310</span><a id="line.310">     * @param rsh The handler used to create the result object from</a>
<span class="sourceLineNo">311</span><a id="line.311">     * the {@code ResultSet}.</a>
<span class="sourceLineNo">312</span><a id="line.312">     * @param params Initialize the PreparedStatement's IN parameters with</a>
<span class="sourceLineNo">313</span><a id="line.313">     * this array.</a>
<span class="sourceLineNo">314</span><a id="line.314">     * @return An object generated by the handler.</a>
<span class="sourceLineNo">315</span><a id="line.315">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">316</span><a id="line.316">     */</a>
<span class="sourceLineNo">317</span><a id="line.317">    public &lt;T&gt; T query(final String sql, final ResultSetHandler&lt;T&gt; rsh, final Object... params) throws SQLException {</a>
<span class="sourceLineNo">318</span><a id="line.318">        try (final Connection conn = this.prepareConnection()) {</a>
<span class="sourceLineNo">319</span><a id="line.319">            return this.&lt;T&gt;query(conn, true, sql, rsh, params);</a>
<span class="sourceLineNo">320</span><a id="line.320">        }</a>
<span class="sourceLineNo">321</span><a id="line.321">    }</a>
<span class="sourceLineNo">322</span><a id="line.322"></a>
<span class="sourceLineNo">323</span><a id="line.323">    /**</a>
<span class="sourceLineNo">324</span><a id="line.324">     * Executes the given SELECT SQL without any replacement parameters.</a>
<span class="sourceLineNo">325</span><a id="line.325">     * The {@code Connection} is retrieved from the</a>
<span class="sourceLineNo">326</span><a id="line.326">     * {@code DataSource} set in the constructor.</a>
<span class="sourceLineNo">327</span><a id="line.327">     * @param &lt;T&gt; The type of object that the handler returns</a>
<span class="sourceLineNo">328</span><a id="line.328">     * @param sql The SQL statement to execute.</a>
<span class="sourceLineNo">329</span><a id="line.329">     * @param rsh The handler used to create the result object from</a>
<span class="sourceLineNo">330</span><a id="line.330">     * the {@code ResultSet}.</a>
<span class="sourceLineNo">331</span><a id="line.331">     *</a>
<span class="sourceLineNo">332</span><a id="line.332">     * @return An object generated by the handler.</a>
<span class="sourceLineNo">333</span><a id="line.333">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">334</span><a id="line.334">     */</a>
<span class="sourceLineNo">335</span><a id="line.335">    public &lt;T&gt; T query(final String sql, final ResultSetHandler&lt;T&gt; rsh) throws SQLException {</a>
<span class="sourceLineNo">336</span><a id="line.336">        try (final Connection conn = this.prepareConnection()) {</a>
<span class="sourceLineNo">337</span><a id="line.337">            return this.&lt;T&gt;query(conn, true, sql, rsh, (Object[]) null);</a>
<span class="sourceLineNo">338</span><a id="line.338">        }</a>
<span class="sourceLineNo">339</span><a id="line.339">    }</a>
<span class="sourceLineNo">340</span><a id="line.340"></a>
<span class="sourceLineNo">341</span><a id="line.341">    /**</a>
<span class="sourceLineNo">342</span><a id="line.342">     * Calls query after checking the parameters to ensure nothing is null.</a>
<span class="sourceLineNo">343</span><a id="line.343">     * @param conn The connection to use for the query call.</a>
<span class="sourceLineNo">344</span><a id="line.344">     * @param closeConn True if the connection should be closed, false otherwise.</a>
<span class="sourceLineNo">345</span><a id="line.345">     * @param sql The SQL statement to execute.</a>
<span class="sourceLineNo">346</span><a id="line.346">     * @param params An array of query replacement parameters.  Each row in</a>
<span class="sourceLineNo">347</span><a id="line.347">     * this array is one set of batch replacement values.</a>
<span class="sourceLineNo">348</span><a id="line.348">     * @return The results of the query.</a>
<span class="sourceLineNo">349</span><a id="line.349">     * @throws SQLException If there are database or parameter errors.</a>
<span class="sourceLineNo">350</span><a id="line.350">     */</a>
<span class="sourceLineNo">351</span><a id="line.351">    private &lt;T&gt; T query(final Connection conn, final boolean closeConn, final String sql, final ResultSetHandler&lt;T&gt; rsh, final Object... params)</a>
<span class="sourceLineNo">352</span><a id="line.352">            throws SQLException {</a>
<span class="sourceLineNo">353</span><a id="line.353">        if (conn == null) {</a>
<span class="sourceLineNo">354</span><a id="line.354">            throw new SQLException("Null connection");</a>
<span class="sourceLineNo">355</span><a id="line.355">        }</a>
<span class="sourceLineNo">356</span><a id="line.356"></a>
<span class="sourceLineNo">357</span><a id="line.357">        if (sql == null) {</a>
<span class="sourceLineNo">358</span><a id="line.358">            throw new SQLException("Null SQL statement");</a>
<span class="sourceLineNo">359</span><a id="line.359">        }</a>
<span class="sourceLineNo">360</span><a id="line.360"></a>
<span class="sourceLineNo">361</span><a id="line.361">        if (rsh == null) {</a>
<span class="sourceLineNo">362</span><a id="line.362">            throw new SQLException("Null ResultSetHandler");</a>
<span class="sourceLineNo">363</span><a id="line.363">        }</a>
<span class="sourceLineNo">364</span><a id="line.364"></a>
<span class="sourceLineNo">365</span><a id="line.365">        Statement stmt = null;</a>
<span class="sourceLineNo">366</span><a id="line.366">        ResultSet rs = null;</a>
<span class="sourceLineNo">367</span><a id="line.367">        T result = null;</a>
<span class="sourceLineNo">368</span><a id="line.368"></a>
<span class="sourceLineNo">369</span><a id="line.369">        try {</a>
<span class="sourceLineNo">370</span><a id="line.370">            if (params != null &amp;&amp; params.length &gt; 0) {</a>
<span class="sourceLineNo">371</span><a id="line.371">                PreparedStatement ps = this.prepareStatement(conn, sql);</a>
<span class="sourceLineNo">372</span><a id="line.372">                stmt = ps;</a>
<span class="sourceLineNo">373</span><a id="line.373">                this.fillStatement(ps, params);</a>
<span class="sourceLineNo">374</span><a id="line.374">                rs = this.wrap(ps.executeQuery());</a>
<span class="sourceLineNo">375</span><a id="line.375">            } else {</a>
<span class="sourceLineNo">376</span><a id="line.376">                stmt = conn.createStatement();</a>
<span class="sourceLineNo">377</span><a id="line.377">                rs = this.wrap(stmt.executeQuery(sql));</a>
<span class="sourceLineNo">378</span><a id="line.378">            }</a>
<span class="sourceLineNo">379</span><a id="line.379">            result = rsh.handle(rs);</a>
<span class="sourceLineNo">380</span><a id="line.380"></a>
<span class="sourceLineNo">381</span><a id="line.381">        } catch (final SQLException e) {</a>
<span class="sourceLineNo">382</span><a id="line.382">            this.rethrow(e, sql, params);</a>
<span class="sourceLineNo">383</span><a id="line.383"></a>
<span class="sourceLineNo">384</span><a id="line.384">        } finally {</a>
<span class="sourceLineNo">385</span><a id="line.385">            closeQuietly(rs);</a>
<span class="sourceLineNo">386</span><a id="line.386">            closeQuietly(stmt);</a>
<span class="sourceLineNo">387</span><a id="line.387">        }</a>
<span class="sourceLineNo">388</span><a id="line.388"></a>
<span class="sourceLineNo">389</span><a id="line.389">        return result;</a>
<span class="sourceLineNo">390</span><a id="line.390">    }</a>
<span class="sourceLineNo">391</span><a id="line.391"></a>
<span class="sourceLineNo">392</span><a id="line.392">    /**</a>
<span class="sourceLineNo">393</span><a id="line.393">     * Execute an SQL INSERT, UPDATE, or DELETE query without replacement</a>
<span class="sourceLineNo">394</span><a id="line.394">     * parameters.</a>
<span class="sourceLineNo">395</span><a id="line.395">     *</a>
<span class="sourceLineNo">396</span><a id="line.396">     * @param conn The connection to use to run the query.</a>
<span class="sourceLineNo">397</span><a id="line.397">     * @param sql The SQL to execute.</a>
<span class="sourceLineNo">398</span><a id="line.398">     * @return The number of rows updated.</a>
<span class="sourceLineNo">399</span><a id="line.399">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">400</span><a id="line.400">     */</a>
<span class="sourceLineNo">401</span><a id="line.401">    public int update(final Connection conn, final String sql) throws SQLException {</a>
<span class="sourceLineNo">402</span><a id="line.402">        return this.update(conn, false, sql, (Object[]) null);</a>
<span class="sourceLineNo">403</span><a id="line.403">    }</a>
<span class="sourceLineNo">404</span><a id="line.404"></a>
<span class="sourceLineNo">405</span><a id="line.405">    /**</a>
<span class="sourceLineNo">406</span><a id="line.406">     * Execute an SQL INSERT, UPDATE, or DELETE query with a single replacement</a>
<span class="sourceLineNo">407</span><a id="line.407">     * parameter.</a>
<span class="sourceLineNo">408</span><a id="line.408">     *</a>
<span class="sourceLineNo">409</span><a id="line.409">     * @param conn The connection to use to run the query.</a>
<span class="sourceLineNo">410</span><a id="line.410">     * @param sql The SQL to execute.</a>
<span class="sourceLineNo">411</span><a id="line.411">     * @param param The replacement parameter.</a>
<span class="sourceLineNo">412</span><a id="line.412">     * @return The number of rows updated.</a>
<span class="sourceLineNo">413</span><a id="line.413">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">414</span><a id="line.414">     */</a>
<span class="sourceLineNo">415</span><a id="line.415">    public int update(final Connection conn, final String sql, final Object param) throws SQLException {</a>
<span class="sourceLineNo">416</span><a id="line.416">        return this.update(conn, false, sql, param);</a>
<span class="sourceLineNo">417</span><a id="line.417">    }</a>
<span class="sourceLineNo">418</span><a id="line.418"></a>
<span class="sourceLineNo">419</span><a id="line.419">    /**</a>
<span class="sourceLineNo">420</span><a id="line.420">     * Execute an SQL INSERT, UPDATE, or DELETE query.</a>
<span class="sourceLineNo">421</span><a id="line.421">     *</a>
<span class="sourceLineNo">422</span><a id="line.422">     * @param conn The connection to use to run the query.</a>
<span class="sourceLineNo">423</span><a id="line.423">     * @param sql The SQL to execute.</a>
<span class="sourceLineNo">424</span><a id="line.424">     * @param params The query replacement parameters.</a>
<span class="sourceLineNo">425</span><a id="line.425">     * @return The number of rows updated.</a>
<span class="sourceLineNo">426</span><a id="line.426">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">427</span><a id="line.427">     */</a>
<span class="sourceLineNo">428</span><a id="line.428">    public int update(final Connection conn, final String sql, final Object... params) throws SQLException {</a>
<span class="sourceLineNo">429</span><a id="line.429">        return update(conn, false, sql, params);</a>
<span class="sourceLineNo">430</span><a id="line.430">    }</a>
<span class="sourceLineNo">431</span><a id="line.431"></a>
<span class="sourceLineNo">432</span><a id="line.432">    /**</a>
<span class="sourceLineNo">433</span><a id="line.433">     * Executes the given INSERT, UPDATE, or DELETE SQL statement without</a>
<span class="sourceLineNo">434</span><a id="line.434">     * any replacement parameters. The {@code Connection} is retrieved</a>
<span class="sourceLineNo">435</span><a id="line.435">     * from the {@code DataSource} set in the constructor.  This</a>
<span class="sourceLineNo">436</span><a id="line.436">     * {@code Connection} must be in auto-commit mode or the update will</a>
<span class="sourceLineNo">437</span><a id="line.437">     * not be saved.</a>
<span class="sourceLineNo">438</span><a id="line.438">     *</a>
<span class="sourceLineNo">439</span><a id="line.439">     * @param sql The SQL statement to execute.</a>
<span class="sourceLineNo">440</span><a id="line.440">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">441</span><a id="line.441">     * @return The number of rows updated.</a>
<span class="sourceLineNo">442</span><a id="line.442">     */</a>
<span class="sourceLineNo">443</span><a id="line.443">    public int update(final String sql) throws SQLException {</a>
<span class="sourceLineNo">444</span><a id="line.444">        try (final Connection conn = this.prepareConnection()) {</a>
<span class="sourceLineNo">445</span><a id="line.445">            return this.update(conn, true, sql, (Object[]) null);</a>
<span class="sourceLineNo">446</span><a id="line.446">        }</a>
<span class="sourceLineNo">447</span><a id="line.447">    }</a>
<span class="sourceLineNo">448</span><a id="line.448"></a>
<span class="sourceLineNo">449</span><a id="line.449">    /**</a>
<span class="sourceLineNo">450</span><a id="line.450">     * Executes the given INSERT, UPDATE, or DELETE SQL statement with</a>
<span class="sourceLineNo">451</span><a id="line.451">     * a single replacement parameter.  The {@code Connection} is</a>
<span class="sourceLineNo">452</span><a id="line.452">     * retrieved from the {@code DataSource} set in the constructor.</a>
<span class="sourceLineNo">453</span><a id="line.453">     * This {@code Connection} must be in auto-commit mode or the</a>
<span class="sourceLineNo">454</span><a id="line.454">     * update will not be saved.</a>
<span class="sourceLineNo">455</span><a id="line.455">     *</a>
<span class="sourceLineNo">456</span><a id="line.456">     * @param sql The SQL statement to execute.</a>
<span class="sourceLineNo">457</span><a id="line.457">     * @param param The replacement parameter.</a>
<span class="sourceLineNo">458</span><a id="line.458">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">459</span><a id="line.459">     * @return The number of rows updated.</a>
<span class="sourceLineNo">460</span><a id="line.460">     */</a>
<span class="sourceLineNo">461</span><a id="line.461">    public int update(final String sql, final Object param) throws SQLException {</a>
<span class="sourceLineNo">462</span><a id="line.462">        try (final Connection conn = this.prepareConnection()) {</a>
<span class="sourceLineNo">463</span><a id="line.463">            return this.update(conn, true, sql, param);</a>
<span class="sourceLineNo">464</span><a id="line.464">        }</a>
<span class="sourceLineNo">465</span><a id="line.465">    }</a>
<span class="sourceLineNo">466</span><a id="line.466"></a>
<span class="sourceLineNo">467</span><a id="line.467">    /**</a>
<span class="sourceLineNo">468</span><a id="line.468">     * Executes the given INSERT, UPDATE, or DELETE SQL statement.  The</a>
<span class="sourceLineNo">469</span><a id="line.469">     * {@code Connection} is retrieved from the {@code DataSource}</a>
<span class="sourceLineNo">470</span><a id="line.470">     * set in the constructor.  This {@code Connection} must be in</a>
<span class="sourceLineNo">471</span><a id="line.471">     * auto-commit mode or the update will not be saved.</a>
<span class="sourceLineNo">472</span><a id="line.472">     *</a>
<span class="sourceLineNo">473</span><a id="line.473">     * @param sql The SQL statement to execute.</a>
<span class="sourceLineNo">474</span><a id="line.474">     * @param params Initializes the PreparedStatement's IN (i.e. '?')</a>
<span class="sourceLineNo">475</span><a id="line.475">     * parameters.</a>
<span class="sourceLineNo">476</span><a id="line.476">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">477</span><a id="line.477">     * @return The number of rows updated.</a>
<span class="sourceLineNo">478</span><a id="line.478">     */</a>
<span class="sourceLineNo">479</span><a id="line.479">    public int update(final String sql, final Object... params) throws SQLException {</a>
<span class="sourceLineNo">480</span><a id="line.480">        try (final Connection conn = this.prepareConnection()) {</a>
<span class="sourceLineNo">481</span><a id="line.481">            return this.update(conn, true, sql, params);</a>
<span class="sourceLineNo">482</span><a id="line.482">        }</a>
<span class="sourceLineNo">483</span><a id="line.483">    }</a>
<span class="sourceLineNo">484</span><a id="line.484"></a>
<span class="sourceLineNo">485</span><a id="line.485">    /**</a>
<span class="sourceLineNo">486</span><a id="line.486">     * Calls update after checking the parameters to ensure nothing is null.</a>
<span class="sourceLineNo">487</span><a id="line.487">     * @param conn The connection to use for the update call.</a>
<span class="sourceLineNo">488</span><a id="line.488">     * @param closeConn True if the connection should be closed, false otherwise.</a>
<span class="sourceLineNo">489</span><a id="line.489">     * @param sql The SQL statement to execute.</a>
<span class="sourceLineNo">490</span><a id="line.490">     * @param params An array of update replacement parameters.  Each row in</a>
<span class="sourceLineNo">491</span><a id="line.491">     * this array is one set of update replacement values.</a>
<span class="sourceLineNo">492</span><a id="line.492">     * @return The number of rows updated.</a>
<span class="sourceLineNo">493</span><a id="line.493">     * @throws SQLException If there are database or parameter errors.</a>
<span class="sourceLineNo">494</span><a id="line.494">     */</a>
<span class="sourceLineNo">495</span><a id="line.495">    private int update(final Connection conn, final boolean closeConn, final String sql, final Object... params) throws SQLException {</a>
<span class="sourceLineNo">496</span><a id="line.496">        if (conn == null) {</a>
<span class="sourceLineNo">497</span><a id="line.497">            throw new SQLException("Null connection");</a>
<span class="sourceLineNo">498</span><a id="line.498">        }</a>
<span class="sourceLineNo">499</span><a id="line.499"></a>
<span class="sourceLineNo">500</span><a id="line.500">        if (sql == null) {</a>
<span class="sourceLineNo">501</span><a id="line.501">            throw new SQLException("Null SQL statement");</a>
<span class="sourceLineNo">502</span><a id="line.502">        }</a>
<span class="sourceLineNo">503</span><a id="line.503"></a>
<span class="sourceLineNo">504</span><a id="line.504">        Statement stmt = null;</a>
<span class="sourceLineNo">505</span><a id="line.505">        int rows = 0;</a>
<span class="sourceLineNo">506</span><a id="line.506"></a>
<span class="sourceLineNo">507</span><a id="line.507">        try {</a>
<span class="sourceLineNo">508</span><a id="line.508">            if (params != null &amp;&amp; params.length &gt; 0) {</a>
<span class="sourceLineNo">509</span><a id="line.509">                PreparedStatement ps = this.prepareStatement(conn, sql);</a>
<span class="sourceLineNo">510</span><a id="line.510">                stmt = ps;</a>
<span class="sourceLineNo">511</span><a id="line.511">                this.fillStatement(ps, params);</a>
<span class="sourceLineNo">512</span><a id="line.512">                rows = ps.executeUpdate();</a>
<span class="sourceLineNo">513</span><a id="line.513">            } else {</a>
<span class="sourceLineNo">514</span><a id="line.514">                stmt = conn.createStatement();</a>
<span class="sourceLineNo">515</span><a id="line.515">                rows = stmt.executeUpdate(sql);</a>
<span class="sourceLineNo">516</span><a id="line.516">            }</a>
<span class="sourceLineNo">517</span><a id="line.517"></a>
<span class="sourceLineNo">518</span><a id="line.518">        } catch (final SQLException e) {</a>
<span class="sourceLineNo">519</span><a id="line.519">            this.rethrow(e, sql, params);</a>
<span class="sourceLineNo">520</span><a id="line.520"></a>
<span class="sourceLineNo">521</span><a id="line.521">        } finally {</a>
<span class="sourceLineNo">522</span><a id="line.522">            close(stmt);</a>
<span class="sourceLineNo">523</span><a id="line.523">        }</a>
<span class="sourceLineNo">524</span><a id="line.524"></a>
<span class="sourceLineNo">525</span><a id="line.525">        return rows;</a>
<span class="sourceLineNo">526</span><a id="line.526">    }</a>
<span class="sourceLineNo">527</span><a id="line.527"></a>
<span class="sourceLineNo">528</span><a id="line.528">    /**</a>
<span class="sourceLineNo">529</span><a id="line.529">     * Executes the given INSERT SQL without any replacement parameters.</a>
<span class="sourceLineNo">530</span><a id="line.530">     * The {@code Connection} is retrieved from the</a>
<span class="sourceLineNo">531</span><a id="line.531">     * {@code DataSource} set in the constructor.</a>
<span class="sourceLineNo">532</span><a id="line.532">     * @param &lt;T&gt; The type of object that the handler returns</a>
<span class="sourceLineNo">533</span><a id="line.533">     * @param sql The SQL statement to execute.</a>
<span class="sourceLineNo">534</span><a id="line.534">     * @param rsh The handler used to create the result object from</a>
<span class="sourceLineNo">535</span><a id="line.535">     * the {@code ResultSet} of auto-generated keys.</a>
<span class="sourceLineNo">536</span><a id="line.536">     * @return An object generated by the handler.</a>
<span class="sourceLineNo">537</span><a id="line.537">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">538</span><a id="line.538">     * @since 1.6</a>
<span class="sourceLineNo">539</span><a id="line.539">     */</a>
<span class="sourceLineNo">540</span><a id="line.540">    public &lt;T&gt; T insert(final String sql, final ResultSetHandler&lt;T&gt; rsh) throws SQLException {</a>
<span class="sourceLineNo">541</span><a id="line.541">        try (final Connection conn = this.prepareConnection()) {</a>
<span class="sourceLineNo">542</span><a id="line.542">            return insert(conn, true, sql, rsh, (Object[]) null);</a>
<span class="sourceLineNo">543</span><a id="line.543">        }</a>
<span class="sourceLineNo">544</span><a id="line.544">    }</a>
<span class="sourceLineNo">545</span><a id="line.545"></a>
<span class="sourceLineNo">546</span><a id="line.546">    /**</a>
<span class="sourceLineNo">547</span><a id="line.547">     * Executes the given INSERT SQL statement. The</a>
<span class="sourceLineNo">548</span><a id="line.548">     * {@code Connection} is retrieved from the {@code DataSource}</a>
<span class="sourceLineNo">549</span><a id="line.549">     * set in the constructor.  This {@code Connection} must be in</a>
<span class="sourceLineNo">550</span><a id="line.550">     * auto-commit mode or the insert will not be saved.</a>
<span class="sourceLineNo">551</span><a id="line.551">     * @param &lt;T&gt; The type of object that the handler returns</a>
<span class="sourceLineNo">552</span><a id="line.552">     * @param sql The SQL statement to execute.</a>
<span class="sourceLineNo">553</span><a id="line.553">     * @param rsh The handler used to create the result object from</a>
<span class="sourceLineNo">554</span><a id="line.554">     * the {@code ResultSet} of auto-generated keys.</a>
<span class="sourceLineNo">555</span><a id="line.555">     * @param params Initializes the PreparedStatement's IN (i.e. '?')</a>
<span class="sourceLineNo">556</span><a id="line.556">     * @return An object generated by the handler.</a>
<span class="sourceLineNo">557</span><a id="line.557">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">558</span><a id="line.558">     * @since 1.6</a>
<span class="sourceLineNo">559</span><a id="line.559">     */</a>
<span class="sourceLineNo">560</span><a id="line.560">    public &lt;T&gt; T insert(final String sql, final ResultSetHandler&lt;T&gt; rsh, final Object... params) throws SQLException {</a>
<span class="sourceLineNo">561</span><a id="line.561">        try (final Connection conn = this.prepareConnection()) {</a>
<span class="sourceLineNo">562</span><a id="line.562">            return insert(conn, true, sql, rsh, params);</a>
<span class="sourceLineNo">563</span><a id="line.563">        }</a>
<span class="sourceLineNo">564</span><a id="line.564">    }</a>
<span class="sourceLineNo">565</span><a id="line.565"></a>
<span class="sourceLineNo">566</span><a id="line.566">    /**</a>
<span class="sourceLineNo">567</span><a id="line.567">     * Execute an SQL INSERT query without replacement parameters.</a>
<span class="sourceLineNo">568</span><a id="line.568">     * @param &lt;T&gt; The type of object that the handler returns</a>
<span class="sourceLineNo">569</span><a id="line.569">     * @param conn The connection to use to run the query.</a>
<span class="sourceLineNo">570</span><a id="line.570">     * @param sql The SQL to execute.</a>
<span class="sourceLineNo">571</span><a id="line.571">     * @param rsh The handler used to create the result object from</a>
<span class="sourceLineNo">572</span><a id="line.572">     * the {@code ResultSet} of auto-generated keys.</a>
<span class="sourceLineNo">573</span><a id="line.573">     * @return An object generated by the handler.</a>
<span class="sourceLineNo">574</span><a id="line.574">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">575</span><a id="line.575">     * @since 1.6</a>
<span class="sourceLineNo">576</span><a id="line.576">     */</a>
<span class="sourceLineNo">577</span><a id="line.577">    public &lt;T&gt; T insert(final Connection conn, final String sql, final ResultSetHandler&lt;T&gt; rsh) throws SQLException {</a>
<span class="sourceLineNo">578</span><a id="line.578">        return insert(conn, false, sql, rsh, (Object[]) null);</a>
<span class="sourceLineNo">579</span><a id="line.579">    }</a>
<span class="sourceLineNo">580</span><a id="line.580"></a>
<span class="sourceLineNo">581</span><a id="line.581">    /**</a>
<span class="sourceLineNo">582</span><a id="line.582">     * Execute an SQL INSERT query.</a>
<span class="sourceLineNo">583</span><a id="line.583">     * @param &lt;T&gt; The type of object that the handler returns</a>
<span class="sourceLineNo">584</span><a id="line.584">     * @param conn The connection to use to run the query.</a>
<span class="sourceLineNo">585</span><a id="line.585">     * @param sql The SQL to execute.</a>
<span class="sourceLineNo">586</span><a id="line.586">     * @param rsh The handler used to create the result object from</a>
<span class="sourceLineNo">587</span><a id="line.587">     * the {@code ResultSet} of auto-generated keys.</a>
<span class="sourceLineNo">588</span><a id="line.588">     * @param params The query replacement parameters.</a>
<span class="sourceLineNo">589</span><a id="line.589">     * @return An object generated by the handler.</a>
<span class="sourceLineNo">590</span><a id="line.590">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">591</span><a id="line.591">     * @since 1.6</a>
<span class="sourceLineNo">592</span><a id="line.592">     */</a>
<span class="sourceLineNo">593</span><a id="line.593">    public &lt;T&gt; T insert(final Connection conn, final String sql, final ResultSetHandler&lt;T&gt; rsh, final Object... params) throws SQLException {</a>
<span class="sourceLineNo">594</span><a id="line.594">        return insert(conn, false, sql, rsh, params);</a>
<span class="sourceLineNo">595</span><a id="line.595">    }</a>
<span class="sourceLineNo">596</span><a id="line.596"></a>
<span class="sourceLineNo">597</span><a id="line.597">    /**</a>
<span class="sourceLineNo">598</span><a id="line.598">     * Executes the given INSERT SQL statement.</a>
<span class="sourceLineNo">599</span><a id="line.599">     * @param conn The connection to use for the query call.</a>
<span class="sourceLineNo">600</span><a id="line.600">     * @param closeConn True if the connection should be closed, false otherwise.</a>
<span class="sourceLineNo">601</span><a id="line.601">     * @param sql The SQL statement to execute.</a>
<span class="sourceLineNo">602</span><a id="line.602">     * @param rsh The handler used to create the result object from</a>
<span class="sourceLineNo">603</span><a id="line.603">     * the {@code ResultSet} of auto-generated keys.</a>
<span class="sourceLineNo">604</span><a id="line.604">     * @param params The query replacement parameters.</a>
<span class="sourceLineNo">605</span><a id="line.605">     * @return An object generated by the handler.</a>
<span class="sourceLineNo">606</span><a id="line.606">     * @throws SQLException If there are database or parameter errors.</a>
<span class="sourceLineNo">607</span><a id="line.607">     * @since 1.6</a>
<span class="sourceLineNo">608</span><a id="line.608">     */</a>
<span class="sourceLineNo">609</span><a id="line.609">    private &lt;T&gt; T insert(final Connection conn, final boolean closeConn, final String sql, final ResultSetHandler&lt;T&gt; rsh, final Object... params)</a>
<span class="sourceLineNo">610</span><a id="line.610">            throws SQLException {</a>
<span class="sourceLineNo">611</span><a id="line.611">        if (conn == null) {</a>
<span class="sourceLineNo">612</span><a id="line.612">            throw new SQLException("Null connection");</a>
<span class="sourceLineNo">613</span><a id="line.613">        }</a>
<span class="sourceLineNo">614</span><a id="line.614"></a>
<span class="sourceLineNo">615</span><a id="line.615">        if (sql == null) {</a>
<span class="sourceLineNo">616</span><a id="line.616">            throw new SQLException("Null SQL statement");</a>
<span class="sourceLineNo">617</span><a id="line.617">        }</a>
<span class="sourceLineNo">618</span><a id="line.618"></a>
<span class="sourceLineNo">619</span><a id="line.619">        if (rsh == null) {</a>
<span class="sourceLineNo">620</span><a id="line.620">            throw new SQLException("Null ResultSetHandler");</a>
<span class="sourceLineNo">621</span><a id="line.621">        }</a>
<span class="sourceLineNo">622</span><a id="line.622"></a>
<span class="sourceLineNo">623</span><a id="line.623">        Statement stmt = null;</a>
<span class="sourceLineNo">624</span><a id="line.624">        T generatedKeys = null;</a>
<span class="sourceLineNo">625</span><a id="line.625"></a>
<span class="sourceLineNo">626</span><a id="line.626">        try {</a>
<span class="sourceLineNo">627</span><a id="line.627">            if (params != null &amp;&amp; params.length &gt; 0) {</a>
<span class="sourceLineNo">628</span><a id="line.628">                PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);</a>
<span class="sourceLineNo">629</span><a id="line.629">                stmt = ps;</a>
<span class="sourceLineNo">630</span><a id="line.630">                this.fillStatement(ps, params);</a>
<span class="sourceLineNo">631</span><a id="line.631">                ps.executeUpdate();</a>
<span class="sourceLineNo">632</span><a id="line.632">            } else {</a>
<span class="sourceLineNo">633</span><a id="line.633">                stmt = conn.createStatement();</a>
<span class="sourceLineNo">634</span><a id="line.634">                stmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);</a>
<span class="sourceLineNo">635</span><a id="line.635">            }</a>
<span class="sourceLineNo">636</span><a id="line.636">            final ResultSet resultSet = stmt.getGeneratedKeys();</a>
<span class="sourceLineNo">637</span><a id="line.637">            generatedKeys = rsh.handle(resultSet);</a>
<span class="sourceLineNo">638</span><a id="line.638">        } catch (final SQLException e) {</a>
<span class="sourceLineNo">639</span><a id="line.639">            this.rethrow(e, sql, params);</a>
<span class="sourceLineNo">640</span><a id="line.640">        } finally {</a>
<span class="sourceLineNo">641</span><a id="line.641">            close(stmt);</a>
<span class="sourceLineNo">642</span><a id="line.642">        }</a>
<span class="sourceLineNo">643</span><a id="line.643"></a>
<span class="sourceLineNo">644</span><a id="line.644">        return generatedKeys;</a>
<span class="sourceLineNo">645</span><a id="line.645">    }</a>
<span class="sourceLineNo">646</span><a id="line.646"></a>
<span class="sourceLineNo">647</span><a id="line.647">    /**</a>
<span class="sourceLineNo">648</span><a id="line.648">     * Executes the given batch of INSERT SQL statements. The</a>
<span class="sourceLineNo">649</span><a id="line.649">     * {@code Connection} is retrieved from the {@code DataSource}</a>
<span class="sourceLineNo">650</span><a id="line.650">     * set in the constructor.  This {@code Connection} must be in</a>
<span class="sourceLineNo">651</span><a id="line.651">     * auto-commit mode or the insert will not be saved.</a>
<span class="sourceLineNo">652</span><a id="line.652">     * @param &lt;T&gt; The type of object that the handler returns</a>
<span class="sourceLineNo">653</span><a id="line.653">     * @param sql The SQL statement to execute.</a>
<span class="sourceLineNo">654</span><a id="line.654">     * @param rsh The handler used to create the result object from</a>
<span class="sourceLineNo">655</span><a id="line.655">     * the {@code ResultSet} of auto-generated keys.</a>
<span class="sourceLineNo">656</span><a id="line.656">     * @param params Initializes the PreparedStatement's IN (i.e. '?')</a>
<span class="sourceLineNo">657</span><a id="line.657">     * @return The result generated by the handler.</a>
<span class="sourceLineNo">658</span><a id="line.658">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">659</span><a id="line.659">     * @since 1.6</a>
<span class="sourceLineNo">660</span><a id="line.660">     */</a>
<span class="sourceLineNo">661</span><a id="line.661">    public &lt;T&gt; T insertBatch(final String sql, final ResultSetHandler&lt;T&gt; rsh, final Object[][] params) throws SQLException {</a>
<span class="sourceLineNo">662</span><a id="line.662">        try (final Connection conn = this.prepareConnection()) {</a>
<span class="sourceLineNo">663</span><a id="line.663">            return insertBatch(conn, true, sql, rsh, params);</a>
<span class="sourceLineNo">664</span><a id="line.664">        }</a>
<span class="sourceLineNo">665</span><a id="line.665">    }</a>
<span class="sourceLineNo">666</span><a id="line.666"></a>
<span class="sourceLineNo">667</span><a id="line.667">    /**</a>
<span class="sourceLineNo">668</span><a id="line.668">     * Executes the given batch of INSERT SQL statements.</a>
<span class="sourceLineNo">669</span><a id="line.669">     * @param &lt;T&gt; The type of object that the handler returns</a>
<span class="sourceLineNo">670</span><a id="line.670">     * @param conn The connection to use to run the query.</a>
<span class="sourceLineNo">671</span><a id="line.671">     * @param sql The SQL to execute.</a>
<span class="sourceLineNo">672</span><a id="line.672">     * @param rsh The handler used to create the result object from</a>
<span class="sourceLineNo">673</span><a id="line.673">     * the {@code ResultSet} of auto-generated keys.</a>
<span class="sourceLineNo">674</span><a id="line.674">     * @param params The query replacement parameters.</a>
<span class="sourceLineNo">675</span><a id="line.675">     * @return The result generated by the handler.</a>
<span class="sourceLineNo">676</span><a id="line.676">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">677</span><a id="line.677">     * @since 1.6</a>
<span class="sourceLineNo">678</span><a id="line.678">     */</a>
<span class="sourceLineNo">679</span><a id="line.679">    public &lt;T&gt; T insertBatch(final Connection conn, final String sql, final ResultSetHandler&lt;T&gt; rsh, final Object[][] params) throws SQLException {</a>
<span class="sourceLineNo">680</span><a id="line.680">        return insertBatch(conn, false, sql, rsh, params);</a>
<span class="sourceLineNo">681</span><a id="line.681">    }</a>
<span class="sourceLineNo">682</span><a id="line.682"></a>
<span class="sourceLineNo">683</span><a id="line.683">    /**</a>
<span class="sourceLineNo">684</span><a id="line.684">     * Executes the given batch of INSERT SQL statements.</a>
<span class="sourceLineNo">685</span><a id="line.685">     * @param conn The connection to use for the query call.</a>
<span class="sourceLineNo">686</span><a id="line.686">     * @param closeConn True if the connection should be closed, false otherwise.</a>
<span class="sourceLineNo">687</span><a id="line.687">     * @param sql The SQL statement to execute.</a>
<span class="sourceLineNo">688</span><a id="line.688">     * @param rsh The handler used to create the result object from</a>
<span class="sourceLineNo">689</span><a id="line.689">     * the {@code ResultSet} of auto-generated keys.</a>
<span class="sourceLineNo">690</span><a id="line.690">     * @param params The query replacement parameters.</a>
<span class="sourceLineNo">691</span><a id="line.691">     * @return The result generated by the handler.</a>
<span class="sourceLineNo">692</span><a id="line.692">     * @throws SQLException If there are database or parameter errors.</a>
<span class="sourceLineNo">693</span><a id="line.693">     * @since 1.6</a>
<span class="sourceLineNo">694</span><a id="line.694">     */</a>
<span class="sourceLineNo">695</span><a id="line.695">    private &lt;T&gt; T insertBatch(final Connection conn, final boolean closeConn, final String sql, final ResultSetHandler&lt;T&gt; rsh, final Object[][] params)</a>
<span class="sourceLineNo">696</span><a id="line.696">            throws SQLException {</a>
<span class="sourceLineNo">697</span><a id="line.697">        if (conn == null) {</a>
<span class="sourceLineNo">698</span><a id="line.698">            throw new SQLException("Null connection");</a>
<span class="sourceLineNo">699</span><a id="line.699">        }</a>
<span class="sourceLineNo">700</span><a id="line.700"></a>
<span class="sourceLineNo">701</span><a id="line.701">        if (sql == null) {</a>
<span class="sourceLineNo">702</span><a id="line.702">            throw new SQLException("Null SQL statement");</a>
<span class="sourceLineNo">703</span><a id="line.703">        }</a>
<span class="sourceLineNo">704</span><a id="line.704"></a>
<span class="sourceLineNo">705</span><a id="line.705">        if (params == null) {</a>
<span class="sourceLineNo">706</span><a id="line.706">            throw new SQLException("Null parameters. If parameters aren't need, pass an empty array.");</a>
<span class="sourceLineNo">707</span><a id="line.707">        }</a>
<span class="sourceLineNo">708</span><a id="line.708"></a>
<span class="sourceLineNo">709</span><a id="line.709">        PreparedStatement stmt = null;</a>
<span class="sourceLineNo">710</span><a id="line.710">        T generatedKeys = null;</a>
<span class="sourceLineNo">711</span><a id="line.711">        try {</a>
<span class="sourceLineNo">712</span><a id="line.712">            stmt = this.prepareStatement(conn, sql, Statement.RETURN_GENERATED_KEYS);</a>
<span class="sourceLineNo">713</span><a id="line.713"></a>
<span class="sourceLineNo">714</span><a id="line.714">            for (Object[] param : params) {</a>
<span class="sourceLineNo">715</span><a id="line.715">                this.fillStatement(stmt, param);</a>
<span class="sourceLineNo">716</span><a id="line.716">                stmt.addBatch();</a>
<span class="sourceLineNo">717</span><a id="line.717">            }</a>
<span class="sourceLineNo">718</span><a id="line.718">            stmt.executeBatch();</a>
<span class="sourceLineNo">719</span><a id="line.719">            final ResultSet rs = stmt.getGeneratedKeys();</a>
<span class="sourceLineNo">720</span><a id="line.720">            generatedKeys = rsh.handle(rs);</a>
<span class="sourceLineNo">721</span><a id="line.721"></a>
<span class="sourceLineNo">722</span><a id="line.722">        } catch (final SQLException e) {</a>
<span class="sourceLineNo">723</span><a id="line.723">            this.rethrow(e, sql, (Object[])params);</a>
<span class="sourceLineNo">724</span><a id="line.724">        } finally {</a>
<span class="sourceLineNo">725</span><a id="line.725">            close(stmt);</a>
<span class="sourceLineNo">726</span><a id="line.726">        }</a>
<span class="sourceLineNo">727</span><a id="line.727"></a>
<span class="sourceLineNo">728</span><a id="line.728">        return generatedKeys;</a>
<span class="sourceLineNo">729</span><a id="line.729">    }</a>
<span class="sourceLineNo">730</span><a id="line.730"></a>
<span class="sourceLineNo">731</span><a id="line.731">    /**</a>
<span class="sourceLineNo">732</span><a id="line.732">     * Execute an SQL statement, including a stored procedure call, which does</a>
<span class="sourceLineNo">733</span><a id="line.733">     * not return any result sets.</a>
<span class="sourceLineNo">734</span><a id="line.734">     * Any parameters which are instances of {@link OutParameter} will be</a>
<span class="sourceLineNo">735</span><a id="line.735">     * registered as OUT parameters.</a>
<span class="sourceLineNo">736</span><a id="line.736">     * &lt;p&gt;</a>
<span class="sourceLineNo">737</span><a id="line.737">     * Use this method when invoking a stored procedure with OUT parameters</a>
<span class="sourceLineNo">738</span><a id="line.738">     * that does not return any result sets.  If you are not invoking a stored</a>
<span class="sourceLineNo">739</span><a id="line.739">     * procedure, or the stored procedure has no OUT parameters, consider using</a>
<span class="sourceLineNo">740</span><a id="line.740">     * {@link #update(java.sql.Connection, java.lang.String, java.lang.Object...) }.</a>
<span class="sourceLineNo">741</span><a id="line.741">     * If the stored procedure returns result sets, use</a>
<span class="sourceLineNo">742</span><a id="line.742">     * {@link #execute(java.sql.Connection, java.lang.String, org.apache.commons.dbutils.ResultSetHandler, java.lang.Object...) }.</a>
<span class="sourceLineNo">743</span><a id="line.743">     *</a>
<span class="sourceLineNo">744</span><a id="line.744">     * @param conn The connection to use to run the query.</a>
<span class="sourceLineNo">745</span><a id="line.745">     * @param sql The SQL to execute.</a>
<span class="sourceLineNo">746</span><a id="line.746">     * @param params The query replacement parameters.</a>
<span class="sourceLineNo">747</span><a id="line.747">     * @return The number of rows updated.</a>
<span class="sourceLineNo">748</span><a id="line.748">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">749</span><a id="line.749">     */</a>
<span class="sourceLineNo">750</span><a id="line.750">    public int execute(final Connection conn, final String sql, final Object... params) throws SQLException {</a>
<span class="sourceLineNo">751</span><a id="line.751">        return this.execute(conn, false, sql, params);</a>
<span class="sourceLineNo">752</span><a id="line.752">    }</a>
<span class="sourceLineNo">753</span><a id="line.753"></a>
<span class="sourceLineNo">754</span><a id="line.754">    /**</a>
<span class="sourceLineNo">755</span><a id="line.755">     * Execute an SQL statement, including a stored procedure call, which does</a>
<span class="sourceLineNo">756</span><a id="line.756">     * not return any result sets.</a>
<span class="sourceLineNo">757</span><a id="line.757">     * Any parameters which are instances of {@link OutParameter} will be</a>
<span class="sourceLineNo">758</span><a id="line.758">     * registered as OUT parameters.</a>
<span class="sourceLineNo">759</span><a id="line.759">     * &lt;p&gt;</a>
<span class="sourceLineNo">760</span><a id="line.760">     * Use this method when invoking a stored procedure with OUT parameters</a>
<span class="sourceLineNo">761</span><a id="line.761">     * that does not return any result sets.  If you are not invoking a stored</a>
<span class="sourceLineNo">762</span><a id="line.762">     * procedure, or the stored procedure has no OUT parameters, consider using</a>
<span class="sourceLineNo">763</span><a id="line.763">     * {@link #update(java.lang.String, java.lang.Object...) }.</a>
<span class="sourceLineNo">764</span><a id="line.764">     * If the stored procedure returns result sets, use</a>
<span class="sourceLineNo">765</span><a id="line.765">     * {@link #execute(java.lang.String, org.apache.commons.dbutils.ResultSetHandler, java.lang.Object...) }.</a>
<span class="sourceLineNo">766</span><a id="line.766">     * &lt;p&gt;</a>
<span class="sourceLineNo">767</span><a id="line.767">     * The {@code Connection} is retrieved from the {@code DataSource}</a>
<span class="sourceLineNo">768</span><a id="line.768">     * set in the constructor.  This {@code Connection} must be in</a>
<span class="sourceLineNo">769</span><a id="line.769">     * auto-commit mode or the update will not be saved.</a>
<span class="sourceLineNo">770</span><a id="line.770">     *</a>
<span class="sourceLineNo">771</span><a id="line.771">     * @param sql The SQL statement to execute.</a>
<span class="sourceLineNo">772</span><a id="line.772">     * @param params Initializes the CallableStatement's parameters (i.e. '?').</a>
<span class="sourceLineNo">773</span><a id="line.773">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">774</span><a id="line.774">     * @return The number of rows updated.</a>
<span class="sourceLineNo">775</span><a id="line.775">     */</a>
<span class="sourceLineNo">776</span><a id="line.776">    public int execute(final String sql, final Object... params) throws SQLException {</a>
<span class="sourceLineNo">777</span><a id="line.777">        try (final Connection conn = this.prepareConnection()) {</a>
<span class="sourceLineNo">778</span><a id="line.778">            return this.execute(conn, true, sql, params);</a>
<span class="sourceLineNo">779</span><a id="line.779">        }</a>
<span class="sourceLineNo">780</span><a id="line.780">    }</a>
<span class="sourceLineNo">781</span><a id="line.781"></a>
<span class="sourceLineNo">782</span><a id="line.782">    /**</a>
<span class="sourceLineNo">783</span><a id="line.783">     * Execute an SQL statement, including a stored procedure call, which</a>
<span class="sourceLineNo">784</span><a id="line.784">     * returns one or more result sets.</a>
<span class="sourceLineNo">785</span><a id="line.785">     * Any parameters which are instances of {@link OutParameter} will be</a>
<span class="sourceLineNo">786</span><a id="line.786">     * registered as OUT parameters.</a>
<span class="sourceLineNo">787</span><a id="line.787">     * &lt;p&gt;</a>
<span class="sourceLineNo">788</span><a id="line.788">     * Use this method when: a) running SQL statements that return multiple</a>
<span class="sourceLineNo">789</span><a id="line.789">     * result sets; b) invoking a stored procedure that return result</a>
<span class="sourceLineNo">790</span><a id="line.790">     * sets and OUT parameters.  Otherwise you may wish to use</a>
<span class="sourceLineNo">791</span><a id="line.791">     * {@link #query(java.sql.Connection, java.lang.String, org.apache.commons.dbutils.ResultSetHandler, java.lang.Object...) }</a>
<span class="sourceLineNo">792</span><a id="line.792">     * (if there are no OUT parameters) or</a>
<span class="sourceLineNo">793</span><a id="line.793">     * {@link #execute(java.sql.Connection, java.lang.String, java.lang.Object...) }</a>
<span class="sourceLineNo">794</span><a id="line.794">     * (if there are no result sets).</a>
<span class="sourceLineNo">795</span><a id="line.795">     *</a>
<span class="sourceLineNo">796</span><a id="line.796">     * @param &lt;T&gt; The type of object that the handler returns</a>
<span class="sourceLineNo">797</span><a id="line.797">     * @param conn The connection to use to run the query.</a>
<span class="sourceLineNo">798</span><a id="line.798">     * @param sql The SQL to execute.</a>
<span class="sourceLineNo">799</span><a id="line.799">     * @param rsh The result set handler</a>
<span class="sourceLineNo">800</span><a id="line.800">     * @param params The query replacement parameters.</a>
<span class="sourceLineNo">801</span><a id="line.801">     * @return A list of objects generated by the handler</a>
<span class="sourceLineNo">802</span><a id="line.802">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">803</span><a id="line.803">     */</a>
<span class="sourceLineNo">804</span><a id="line.804">    public &lt;T&gt; List&lt;T&gt; execute(final Connection conn, final String sql, final ResultSetHandler&lt;T&gt; rsh, final Object... params) throws SQLException {</a>
<span class="sourceLineNo">805</span><a id="line.805">        return this.execute(conn, false, sql, rsh, params);</a>
<span class="sourceLineNo">806</span><a id="line.806">    }</a>
<span class="sourceLineNo">807</span><a id="line.807"></a>
<span class="sourceLineNo">808</span><a id="line.808">    /**</a>
<span class="sourceLineNo">809</span><a id="line.809">     * Execute an SQL statement, including a stored procedure call, which</a>
<span class="sourceLineNo">810</span><a id="line.810">     * returns one or more result sets.</a>
<span class="sourceLineNo">811</span><a id="line.811">     * Any parameters which are instances of {@link OutParameter} will be</a>
<span class="sourceLineNo">812</span><a id="line.812">     * registered as OUT parameters.</a>
<span class="sourceLineNo">813</span><a id="line.813">     * &lt;p&gt;</a>
<span class="sourceLineNo">814</span><a id="line.814">     * Use this method when: a) running SQL statements that return multiple</a>
<span class="sourceLineNo">815</span><a id="line.815">     * result sets; b) invoking a stored procedure that return result</a>
<span class="sourceLineNo">816</span><a id="line.816">     * sets and OUT parameters.  Otherwise you may wish to use</a>
<span class="sourceLineNo">817</span><a id="line.817">     * {@link #query(java.lang.String, org.apache.commons.dbutils.ResultSetHandler, java.lang.Object...) }</a>
<span class="sourceLineNo">818</span><a id="line.818">     * (if there are no OUT parameters) or</a>
<span class="sourceLineNo">819</span><a id="line.819">     * {@link #execute(java.lang.String, java.lang.Object...) }</a>
<span class="sourceLineNo">820</span><a id="line.820">     * (if there are no result sets).</a>
<span class="sourceLineNo">821</span><a id="line.821">     *</a>
<span class="sourceLineNo">822</span><a id="line.822">     * @param &lt;T&gt; The type of object that the handler returns</a>
<span class="sourceLineNo">823</span><a id="line.823">     * @param sql The SQL to execute.</a>
<span class="sourceLineNo">824</span><a id="line.824">     * @param rsh The result set handler</a>
<span class="sourceLineNo">825</span><a id="line.825">     * @param params The query replacement parameters.</a>
<span class="sourceLineNo">826</span><a id="line.826">     * @return A list of objects generated by the handler</a>
<span class="sourceLineNo">827</span><a id="line.827">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">828</span><a id="line.828">     */</a>
<span class="sourceLineNo">829</span><a id="line.829">    public &lt;T&gt; List&lt;T&gt; execute(final String sql, final ResultSetHandler&lt;T&gt; rsh, final Object... params) throws SQLException {</a>
<span class="sourceLineNo">830</span><a id="line.830">        try (final Connection conn = this.prepareConnection()) {</a>
<span class="sourceLineNo">831</span><a id="line.831">            return this.execute(conn, true, sql, rsh, params);</a>
<span class="sourceLineNo">832</span><a id="line.832">        }</a>
<span class="sourceLineNo">833</span><a id="line.833">    }</a>
<span class="sourceLineNo">834</span><a id="line.834"></a>
<span class="sourceLineNo">835</span><a id="line.835">    /**</a>
<span class="sourceLineNo">836</span><a id="line.836">     * Invokes the stored procedure via update after checking the parameters to</a>
<span class="sourceLineNo">837</span><a id="line.837">     * ensure nothing is null.</a>
<span class="sourceLineNo">838</span><a id="line.838">     * @param conn The connection to use for the update call.</a>
<span class="sourceLineNo">839</span><a id="line.839">     * @param closeConn True if the connection should be closed, false otherwise.</a>
<span class="sourceLineNo">840</span><a id="line.840">     * @param sql The SQL statement to execute.</a>
<span class="sourceLineNo">841</span><a id="line.841">     * @param params An array of update replacement parameters.  Each row in</a>
<span class="sourceLineNo">842</span><a id="line.842">     * this array is one set of update replacement values.</a>
<span class="sourceLineNo">843</span><a id="line.843">     * @return The number of rows updated.</a>
<span class="sourceLineNo">844</span><a id="line.844">     * @throws SQLException If there are database or parameter errors.</a>
<span class="sourceLineNo">845</span><a id="line.845">     */</a>
<span class="sourceLineNo">846</span><a id="line.846">    private int execute(final Connection conn, final boolean closeConn, final String sql, final Object... params) throws SQLException {</a>
<span class="sourceLineNo">847</span><a id="line.847">        if (conn == null) {</a>
<span class="sourceLineNo">848</span><a id="line.848">            throw new SQLException("Null connection");</a>
<span class="sourceLineNo">849</span><a id="line.849">        }</a>
<span class="sourceLineNo">850</span><a id="line.850"></a>
<span class="sourceLineNo">851</span><a id="line.851">        if (sql == null) {</a>
<span class="sourceLineNo">852</span><a id="line.852">            throw new SQLException("Null SQL statement");</a>
<span class="sourceLineNo">853</span><a id="line.853">        }</a>
<span class="sourceLineNo">854</span><a id="line.854"></a>
<span class="sourceLineNo">855</span><a id="line.855">        CallableStatement stmt = null;</a>
<span class="sourceLineNo">856</span><a id="line.856">        int rows = 0;</a>
<span class="sourceLineNo">857</span><a id="line.857"></a>
<span class="sourceLineNo">858</span><a id="line.858">        try {</a>
<span class="sourceLineNo">859</span><a id="line.859">            stmt = this.prepareCall(conn, sql);</a>
<span class="sourceLineNo">860</span><a id="line.860">            this.fillStatement(stmt, params);</a>
<span class="sourceLineNo">861</span><a id="line.861">            stmt.execute();</a>
<span class="sourceLineNo">862</span><a id="line.862">            rows = stmt.getUpdateCount();</a>
<span class="sourceLineNo">863</span><a id="line.863">            this.retrieveOutParameters(stmt, params);</a>
<span class="sourceLineNo">864</span><a id="line.864"></a>
<span class="sourceLineNo">865</span><a id="line.865">        } catch (final SQLException e) {</a>
<span class="sourceLineNo">866</span><a id="line.866">            this.rethrow(e, sql, params);</a>
<span class="sourceLineNo">867</span><a id="line.867"></a>
<span class="sourceLineNo">868</span><a id="line.868">        } finally {</a>
<span class="sourceLineNo">869</span><a id="line.869">            close(stmt);</a>
<span class="sourceLineNo">870</span><a id="line.870">        }</a>
<span class="sourceLineNo">871</span><a id="line.871"></a>
<span class="sourceLineNo">872</span><a id="line.872">        return rows;</a>
<span class="sourceLineNo">873</span><a id="line.873">    }</a>
<span class="sourceLineNo">874</span><a id="line.874"></a>
<span class="sourceLineNo">875</span><a id="line.875">    /**</a>
<span class="sourceLineNo">876</span><a id="line.876">     * Invokes the stored procedure via update after checking the parameters to</a>
<span class="sourceLineNo">877</span><a id="line.877">     * ensure nothing is null.</a>
<span class="sourceLineNo">878</span><a id="line.878">     * @param conn The connection to use for the update call.</a>
<span class="sourceLineNo">879</span><a id="line.879">     * @param closeConn True if the connection should be closed, false otherwise.</a>
<span class="sourceLineNo">880</span><a id="line.880">     * @param sql The SQL statement to execute.</a>
<span class="sourceLineNo">881</span><a id="line.881">     * @param rsh The result set handler</a>
<span class="sourceLineNo">882</span><a id="line.882">     * @param params An array of update replacement parameters.  Each row in</a>
<span class="sourceLineNo">883</span><a id="line.883">     * this array is one set of update replacement values.</a>
<span class="sourceLineNo">884</span><a id="line.884">     * @return List of all objects generated by the ResultSetHandler for all result sets handled.</a>
<span class="sourceLineNo">885</span><a id="line.885">     * @throws SQLException If there are database or parameter errors.</a>
<span class="sourceLineNo">886</span><a id="line.886">     */</a>
<span class="sourceLineNo">887</span><a id="line.887">    private &lt;T&gt; List&lt;T&gt; execute(final Connection conn, final boolean closeConn, final String sql, final ResultSetHandler&lt;T&gt; rsh, final Object... params) throws SQLException {</a>
<span class="sourceLineNo">888</span><a id="line.888">        if (conn == null) {</a>
<span class="sourceLineNo">889</span><a id="line.889">            throw new SQLException("Null connection");</a>
<span class="sourceLineNo">890</span><a id="line.890">        }</a>
<span class="sourceLineNo">891</span><a id="line.891"></a>
<span class="sourceLineNo">892</span><a id="line.892">        if (sql == null) {</a>
<span class="sourceLineNo">893</span><a id="line.893">            throw new SQLException("Null SQL statement");</a>
<span class="sourceLineNo">894</span><a id="line.894">        }</a>
<span class="sourceLineNo">895</span><a id="line.895"></a>
<span class="sourceLineNo">896</span><a id="line.896">        if (rsh == null) {</a>
<span class="sourceLineNo">897</span><a id="line.897">            throw new SQLException("Null ResultSetHandler");</a>
<span class="sourceLineNo">898</span><a id="line.898">        }</a>
<span class="sourceLineNo">899</span><a id="line.899"></a>
<span class="sourceLineNo">900</span><a id="line.900">        CallableStatement stmt = null;</a>
<span class="sourceLineNo">901</span><a id="line.901">        final List&lt;T&gt; results = new LinkedList&lt;&gt;();</a>
<span class="sourceLineNo">902</span><a id="line.902"></a>
<span class="sourceLineNo">903</span><a id="line.903">        try {</a>
<span class="sourceLineNo">904</span><a id="line.904">            stmt = this.prepareCall(conn, sql);</a>
<span class="sourceLineNo">905</span><a id="line.905">            this.fillStatement(stmt, params);</a>
<span class="sourceLineNo">906</span><a id="line.906">            boolean moreResultSets = stmt.execute();</a>
<span class="sourceLineNo">907</span><a id="line.907">            // Handle multiple result sets by passing them through the handler</a>
<span class="sourceLineNo">908</span><a id="line.908">            // retaining the final result</a>
<span class="sourceLineNo">909</span><a id="line.909">            ResultSet rs = null;</a>
<span class="sourceLineNo">910</span><a id="line.910">            while (moreResultSets) {</a>
<span class="sourceLineNo">911</span><a id="line.911">                try {</a>
<span class="sourceLineNo">912</span><a id="line.912">                    rs = this.wrap(stmt.getResultSet());</a>
<span class="sourceLineNo">913</span><a id="line.913">                    results.add(rsh.handle(rs));</a>
<span class="sourceLineNo">914</span><a id="line.914">                    moreResultSets = stmt.getMoreResults();</a>
<span class="sourceLineNo">915</span><a id="line.915"></a>
<span class="sourceLineNo">916</span><a id="line.916">                } finally {</a>
<span class="sourceLineNo">917</span><a id="line.917">                    close(rs);</a>
<span class="sourceLineNo">918</span><a id="line.918">                }</a>
<span class="sourceLineNo">919</span><a id="line.919">            }</a>
<span class="sourceLineNo">920</span><a id="line.920">            this.retrieveOutParameters(stmt, params);</a>
<span class="sourceLineNo">921</span><a id="line.921"></a>
<span class="sourceLineNo">922</span><a id="line.922">        } catch (final SQLException e) {</a>
<span class="sourceLineNo">923</span><a id="line.923">            this.rethrow(e, sql, params);</a>
<span class="sourceLineNo">924</span><a id="line.924"></a>
<span class="sourceLineNo">925</span><a id="line.925">        } finally {</a>
<span class="sourceLineNo">926</span><a id="line.926">            close(stmt);</a>
<span class="sourceLineNo">927</span><a id="line.927">        }</a>
<span class="sourceLineNo">928</span><a id="line.928"></a>
<span class="sourceLineNo">929</span><a id="line.929">        return results;</a>
<span class="sourceLineNo">930</span><a id="line.930">    }</a>
<span class="sourceLineNo">931</span><a id="line.931"></a>
<span class="sourceLineNo">932</span><a id="line.932">    /**</a>
<span class="sourceLineNo">933</span><a id="line.933">     * Set the value on all the {@link OutParameter} instances in the</a>
<span class="sourceLineNo">934</span><a id="line.934">     * {@code params} array using the OUT parameter values from the</a>
<span class="sourceLineNo">935</span><a id="line.935">     * {@code stmt}.</a>
<span class="sourceLineNo">936</span><a id="line.936">     * @param stmt the statement from which to retrieve OUT parameter values</a>
<span class="sourceLineNo">937</span><a id="line.937">     * @param params the parameter array for the statement invocation</a>
<span class="sourceLineNo">938</span><a id="line.938">     * @throws SQLException when the value could not be retrieved from the</a>
<span class="sourceLineNo">939</span><a id="line.939">     * statement.</a>
<span class="sourceLineNo">940</span><a id="line.940">     */</a>
<span class="sourceLineNo">941</span><a id="line.941">    private void retrieveOutParameters(final CallableStatement stmt, final Object[] params) throws SQLException {</a>
<span class="sourceLineNo">942</span><a id="line.942">        if (params != null) {</a>
<span class="sourceLineNo">943</span><a id="line.943">            for (int i = 0; i &lt; params.length; i++) {</a>
<span class="sourceLineNo">944</span><a id="line.944">                if (params[i] instanceof OutParameter) {</a>
<span class="sourceLineNo">945</span><a id="line.945">                    ((OutParameter)params[i]).setValue(stmt, i + 1);</a>
<span class="sourceLineNo">946</span><a id="line.946">                }</a>
<span class="sourceLineNo">947</span><a id="line.947">            }</a>
<span class="sourceLineNo">948</span><a id="line.948">        }</a>
<span class="sourceLineNo">949</span><a id="line.949">    }</a>
<span class="sourceLineNo">950</span><a id="line.950">}</a>




























































</pre>
</div>
</main>
</body>
</html>
